package com.stgl.action;


import com.stgl.service.CampusService;
import com.stgl.service.StudentService;
import com.stgl.util.ExcelUtils;
import com.stgl.util.ExportExcel;
import com.stgl.util.MD5;
import net.sf.jxls.exception.ParsePropertyException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/campus")
public class campus {
    @Resource
    private CampusService campusService;



    @RequestMapping("Search_campus.html")
    @ResponseBody
    public Map<String,Object>Search_campus(HttpServletRequest request, HttpServletResponse response){
        String csname="";
        if(request.getParameter("csname")!=null){
            csname=request.getParameter("csname").toString().trim();
        }

        String csnum="";
        if(request.getParameter("csnum")!=null){
            csnum=request.getParameter("csnum").toString().trim();
        }

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("csname",csname);
        gmap.put("csnum",csnum);

        List<Map<String,Object>>list1=campusService.Search_campus(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
        return rmap;
    }



    @RequestMapping("del_campus.html")
    @ResponseBody
    public Map<String,Object> del_campus(HttpServletRequest request, HttpServletResponse response){
        String csid=request.getParameter("csid").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("csid",csid);
        Map<String,Object>getm=campusService.del_campus(gmap);
        return getm;
    }


    /**
     * @author 牟明艳
     * @comments  学校添加
     * @time 2019年7月26日14:35:43
     */
    @RequestMapping("InsertCampus.html")
    public @ResponseBody Map<String,Object> InsertCampus(HttpServletRequest req)throws Exception{
        Map<String,Object>gmap=new HashMap<String,Object>();
        long csid=System.nanoTime();

        gmap.put("csid",csid);
        String csname=req.getParameter("csname").trim();
        gmap.put("csname",csname);

        String pcaid="";
        if(req.getParameter("pcaid")!=null){
            pcaid=req.getParameter("pcaid").toString().trim();
        }
        gmap.put("pcaid",pcaid);

        String pcalv="";
        if(req.getParameter("pcalv")!=null){
            pcalv=req.getParameter("pcalv").toString().trim();
        }
        gmap.put("pcalv",pcalv);

        String csnum="";
        if(req.getParameter("csnum")!=null){
            csnum=req.getParameter("csnum").toString().trim();
        }
        gmap.put("csnum",csnum);

        String csschid="";
        if(req.getParameter("csschid")!=null){
            csschid=req.getParameter("csschid").toString().trim();
        }
        gmap.put("csschid",csschid);

        String csalias="";
        if(req.getParameter("csalias")!=null){
            csalias=req.getParameter("csalias").toString().trim();
        }
        gmap.put("csalias",csalias);

        String cssite="";
        if(req.getParameter("cssite")!=null){
            cssite=req.getParameter("cssite").toString().trim();
        }
        gmap.put("cssite",cssite);

        String csphone="";
        if(req.getParameter("csphone")!=null){
            csphone=req.getParameter("csphone").toString().trim();
        }
        gmap.put("csphone",csphone);

        String csxz="";
        if(req.getParameter("csxz")!=null){
            csxz=req.getParameter("csxz").toString().trim();
        }
        gmap.put("csxz",csxz);

        String csxzphone="";
        if(req.getParameter("csxzphone")!=null){
            csxzphone=req.getParameter("csxzphone").toString().trim();
        }
        gmap.put("csxzphone",csxzphone);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String cssettime=df.format(new Date());
        gmap.put("cssettime",cssettime);

        HttpSession session=((HttpServletRequest)req).getSession();
        if(session.getAttribute("usid")!=null){
            gmap.put("usid",session.getAttribute("usid").toString());
        }

        Map<String,Object>rmap=new HashMap<String,Object>();
        int m=campusService.InsertCampus(gmap);
        if(m>0){
            rmap.put("result",true);
            rmap.put("msg","保存成功!");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","添加失败,请联系管理员");
        return rmap;
    }




    /**
     * @author 牟明艳
     * @comments  学校修改
     * @time 2019年7月26日14:35:43
     */
    @RequestMapping("EditCampus.html")
    public @ResponseBody Map<String,Object> EditCampus(HttpServletRequest req)throws Exception{
        Map<String,Object>gmap=new HashMap<String,Object>();
        String csid=req.getParameter("csid");
        gmap.put("csid",csid);

        String csname="";
        if(req.getParameter("csname")!=null){
            csname=req.getParameter("csname").trim();
        }
        gmap.put("csname",csname);

        String pcaid="";
        if(req.getParameter("pcaid")!=null){
            pcaid=req.getParameter("pcaid").toString().trim();
        }
        gmap.put("pcaid",pcaid);

        String pcalv="";
        if(req.getParameter("pcalv")!=null){
            pcalv=req.getParameter("pcalv").toString().trim();
        }
        gmap.put("pcalv",pcalv);

        String csnum="";
        if(req.getParameter("csnum")!=null){
            csnum=req.getParameter("csnum").toString().trim();
        }
        gmap.put("csnum",csnum);

        String csschid="";
        if(req.getParameter("csschid")!=null){
            csschid=req.getParameter("csschid").toString().trim();
        }
        gmap.put("csschid",csschid);

        String csalias="";
        if(req.getParameter("csalias")!=null){
            csalias=req.getParameter("csalias").toString().trim();
        }
        gmap.put("csalias",csalias);

        String cssite="";
        if(req.getParameter("cssite")!=null){
            cssite=req.getParameter("cssite").toString().trim();
        }
        gmap.put("cssite",cssite);

        String csphone="";
        if(req.getParameter("csphone")!=null){
            csphone=req.getParameter("csphone").toString().trim();
        }
        gmap.put("csphone",csphone);

        String csxz="";
        if(req.getParameter("csxz")!=null){
            csxz=req.getParameter("csxz").toString().trim();
        }
        gmap.put("csxz",csxz);

        String csxzphone="";
        if(req.getParameter("csxzphone")!=null){
            csxzphone=req.getParameter("csxzphone").toString().trim();
        }
        gmap.put("csxzphone",csxzphone);

        String bmstate="";
        if(req.getParameter("bmstate")!=null){
            bmstate=req.getParameter("bmstate").toString().trim();
        }
        gmap.put("bmstate",bmstate);

        Map<String,Object>rmap=new HashMap<String,Object>();
        int m=campusService.EditCampus(gmap);
        if(m>0){
            rmap.put("result",true);
            rmap.put("msg","保存成功");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","修改失败,请联系管理员!");
        return rmap;
    }







    @RequestMapping("Search_User.html")
    @ResponseBody
    public Map<String,Object>Search_User(HttpServletRequest request, HttpServletResponse response){
        String csname="";
        if(request.getParameter("csname")!=null){
            csname=request.getParameter("csname").toString().trim();
        }

        String usname="";
        if(request.getParameter("usname")!=null){
            usname=request.getParameter("usname").toString().trim();
        }

        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("csname",csname);
        gmap.put("usname",usname);

        List<Map<String,Object>>list1=campusService.Search_User(gmap);
        Map<String,Object>rmap=new HashMap<String,Object>();
        rmap.put("data",list1);
        return rmap;
    }



    /**
     * @author 牟明艳
     * @comments  账号添加
     * @time 2019年7月26日14:35:43
     */
    @RequestMapping("InsertUsers.html")
    public @ResponseBody Map<String,Object> InsertUsers(HttpServletRequest req)throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        long usid = System.nanoTime();
        gmap.put("usid", usid);

        String csid = "";
        if (req.getParameter("csid") != null) {
            csid = req.getParameter("csid").toString().trim();
        }
        gmap.put("csid", csid);

        String usname = "";
        if (req.getParameter("usname") != null) {
            usname = req.getParameter("usname").toString().trim();
        }
        gmap.put("usname", usname);
        gmap.put("usstate", "1");
        gmap.put("uspwd", MD5.MD5("000000"));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("ussettime",df.format(new Date()));

        String pcaid = "";
        if (req.getParameter("pcaid") != null) {
            pcaid = req.getParameter("pcaid").toString().trim();
        }
        gmap.put("pcaid", pcaid);


        String pcalv = "";
        if (req.getParameter("pcalv") != null) {
            pcalv = req.getParameter("pcalv").toString().trim();
        }
        gmap.put("pcalv", pcalv);

        Map<String, Object>rmap=new HashMap<String, Object>();
        int i=campusService.InsertUsers(gmap);
        if(i>0){
            rmap.put("result",true);
            rmap.put("msg","保存成功！");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","保存失败,请联系学校管理员！");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments  账号修改
     * @time 2019年7月26日17:44:26
     */
    @RequestMapping("updateUsers.html")
    public @ResponseBody Map<String,Object> updateUsers(HttpServletRequest req)throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String usid = "";
        if (req.getParameter("usid") != null) {
            usid = req.getParameter("usid").toString().trim();
        }
        gmap.put("usid", usid);

        String csid = "";
        if (req.getParameter("csid") != null) {
            csid = req.getParameter("csid").toString().trim();
        }
        gmap.put("csid", csid);

        String usname = "";
        if (req.getParameter("usname") != null) {
            usname = req.getParameter("usname").toString().trim();
        }
        gmap.put("usname", usname);

        String pcaid = "";
        if (req.getParameter("pcaid") != null) {
            pcaid = req.getParameter("pcaid").toString().trim();
        }
        gmap.put("pcaid", pcaid);

        String pcalv = "";
        if (req.getParameter("pcalv") != null) {
            pcalv = req.getParameter("pcalv").toString().trim();
        }
        gmap.put("pcalv", pcalv);

        Map<String, Object>rmap=new HashMap<String, Object>();
        int i=campusService.updateUsers(gmap);
        if(i>0){
            rmap.put("result",true);
            rmap.put("msg","修改成功！");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","修改失败,请联系学校管理员！");
        return rmap;
    }




    @RequestMapping("del_Users.html")
    @ResponseBody
    public Map<String,Object> del_Users(HttpServletRequest request, HttpServletResponse response){
        String usid=request.getParameter("usid").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("usid",usid);
        Map<String,Object>getm=campusService.del_Users(gmap);
        return getm;
    }





    @RequestMapping("sendmessage.html")
    @ResponseBody
    public void sendmessage(HttpServletRequest request, HttpServletResponse response){
        campusService.sendmessage();
    }


    @RequestMapping("readfile.html")
    @ResponseBody
    public String readfile(HttpServletRequest request, HttpServletResponse response){
        return campusService.readfile();
    }




}
